package com.ztc.ZtcTransportPercentageScale.web;


import com.aqgj.User.bean.User;
import com.aqgj.util.JsonWrapper;
import com.ztc.ZtcTransport.bean.ZtcTransport;
import com.ztc.ZtcTransport.service.ZtcTransportManager;
import com.ztc.ZtcTransportPercentageScale.bean.ZtcTransportPercentageScale;
import com.ztc.ZtcTransportPercentageScale.bean.ZtcTransportPercentageVo;
import com.ztc.ZtcTransportPercentageScale.service.ZtcTransportPercentageScaleManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;

/**
 * 公司提成收益比例
 * @author 2017/11/19
 */
@Controller
@RequestMapping("ztc/ztcTransportPercentageScale")
public class ZtcTransportPercentageScaleAction {

    @Autowired
    private ZtcTransportPercentageScaleManager ztcTransportPercentageScaleManager;
    @Autowired
    private ZtcTransportManager ztcTransportManager;


    /**
     * 获取当前公司旗下公司提成比例信息.
     *
     * @param session
     * @return
     */
    @RequestMapping("getCurrIncludePercentageScaleInfo")
    @ResponseBody
    public HashMap<String, Object> getCurrIncludePercentageScaleInfo(HttpSession session) {
        User user = (User) session.getAttribute("userInfo");
        if (user.getTransportId() == null) {
            return JsonWrapper.failureWrapperMsg("当前公司信息不完整！");
        }
        ZtcTransport ztcTransport = ztcTransportManager.get(user.getTransportId());
        if (ztcTransport == null) {
            return JsonWrapper.failureWrapperMsg("当前公司获取信息失败！");
        }
        return ztcTransportPercentageScaleManager.getPercentageScaleInfo(ztcTransport);
    }

    /**
     * 修改公司对应比例.
     *
     * @param cid   公司ID
     * @param scale 比例值
     * @return
     */
    @RequestMapping("modifyIncludePercentageScale")
    @ResponseBody
    @Transactional
    public HashMap<String, Object> modifyIncludePercentageScale(HttpSession session, String cid, BigDecimal scale, BigDecimal userScale) {
        User user = (User) session.getAttribute("userInfo");
        if (cid == null || scale == null) {
            return JsonWrapper.failureWrapperMsg("设置失败，参数不完整！");
        }
        if (scale.compareTo(new BigDecimal(0)) == -1) {
            return JsonWrapper.failureWrapperMsg("比例输入不能小于0！");
        }
        if (scale.compareTo(new BigDecimal(100)) == 1) {
            return JsonWrapper.failureWrapperMsg("比例不能超过100%！");
        }
        ZtcTransport ztcTransport = ztcTransportManager.get(cid);
        if (ztcTransport == null) {
            return JsonWrapper.failureWrapperMsg("设置失败，获取公司信息失败！");
        }
        ZtcTransport ztcTransport1 = ztcTransportManager.get(user.getTransportId());
        if (ztcTransport1 == null) {
            return JsonWrapper.failureWrapperMsg("设置失败，获取公司信息失败！");
        }
        if (ztcTransport1.getIsPermission() == 1) {
            return JsonWrapper.failureWrapperMsg("设置失败，比例修改权限不足！");
        }
        if (ztcTransport1.getStatus() == 4) {
            return JsonWrapper.failureWrapperMsg("公司处于冻结状态，该操作不被允许！");
        }
        ZtcTransportPercentageScale ztcTransportPercentageScale = ztcTransportPercentageScaleManager.findUniqueBy("transportId", cid);
        if (ztcTransportPercentageScale == null) {
            return JsonWrapper.failureWrapperMsg("未知错误，请稍后重试！");
        }
        BigDecimal oldScale = ztcTransportPercentageScale.getIncomeScale();
        ztcTransportPercentageScale.setIncomeScale(scale);
        ztcTransportPercentageScale.setModifyUserId(user.getId());
        ztcTransportPercentageScale.setModifyTime(new Date());
        ztcTransportPercentageScale.setModifyUsername(user.getAccount());
        ztcTransportPercentageScale.setModifyTransportId(ztcTransport1.getId());
        ztcTransportPercentageScale.setModifyTransportName(ztcTransport1.getFullName());
        if (userScale != null) {
            if (userScale.compareTo(new BigDecimal(0)) == -1) {
                return JsonWrapper.failureWrapperMsg("比例输入不能小于0！");
            }
            if (userScale.compareTo(new BigDecimal(100)) == 1) {
                return JsonWrapper.failureWrapperMsg("比例不能超过100%！");
            }
            BigDecimal oldUserScale = ztcTransport.getDistributorRebateProportion();
            if (ztcTransport.getType() == 3) {
                ztcTransport.setDistributorRebateProportion(userScale);
                ztcTransportManager.update(ztcTransport);
            }
            ztcTransportPercentageScale.setModifyRemark("提成比例由 " + oldScale + " 调整为" + scale+" ,一级分销比例由 "+oldUserScale+" 调整为" + userScale);
        }else{
            ztcTransportPercentageScale.setModifyRemark("比例由 " + oldScale + " 调整为" + scale);
        }
        ztcTransportPercentageScaleManager.update(ztcTransportPercentageScale);
        return JsonWrapper.successWrapper("设置操作成功！");
    }

    /**
     * 设置旗下公司是否有修改比例的权限.
     *
     * @param cid          公司ID
     * @param isPermission （1锁定，2不锁定）
     * @return
     */
    @RequestMapping("setModifyScalePermission")
    @ResponseBody
    public HashMap<String, Object> setModifyScalePermission(String cid, Integer isPermission) {
        if (cid == null || isPermission == null) {
            return JsonWrapper.failureWrapperMsg("设置失败，参数不完整！");
        }
        if (isPermission != 1 && isPermission != 2) {
            return JsonWrapper.failureWrapperMsg("设置失败，参数错误！");
        }
        ZtcTransport ztcTransport = ztcTransportManager.get(cid);
        if (ztcTransport == null) {
            return JsonWrapper.failureWrapperMsg("设置失败，获取公司信息失败！");
        }
        ztcTransport.setIsPermission(isPermission);
        ztcTransportManager.update(ztcTransport);
        return JsonWrapper.successWrapper("设置操作成功！");
    }

}